/* 
 * File:   BlockingQueue.cpp
 * Author: lgr013
 * 
 * Created on 31 de octubre de 2014, 16:44
 */

#include "BlockingQueueEvent.h"

BlockingQueueEvent::BlockingQueueEvent() {
    //    this->pushing = false;
}

void BlockingQueueEvent::push(struct Event data) {
    std::lock_guard<std::mutex> lock(this->mtx);
    this->queue.push(data);
    //    printf("push end\n");

    this->cv.notify_all();
}

struct Event BlockingQueueEvent::pop() {

}

struct Event BlockingQueueEvent::waitAndPop(bool init) {
    this->flag = init;
    std::unique_lock<std::mutex> uni_lock(this->mtx);
    cv.wait(uni_lock, [this] {
        if (!this->queue.empty()) {
            return true;
        }
        if (!this->flag) {
            return true;
        }
        return false;
        //        return !this->queue.empty();
    });

    struct Event data = this->queue.front();
    this->queue.pop();

    return data;
}

bool BlockingQueueEvent::tryPop(struct Event &data) {
    std::unique_lock<std::mutex> uni_lock(this->mtx);
    if (this->queue.empty()) {
        return false;
    }

    data = this->queue.front();
    this->queue.pop();
//    printf("Return event!!!!!!\n");
    return true;
}

bool BlockingQueueEvent::empty() {
    std::lock_guard<std::mutex> lock(this->mtx);
    return this->queue.empty();
}

int BlockingQueueEvent::size() {
    return this->queue.size();
}

BlockingQueueEvent::~BlockingQueueEvent() {
}

